<template>
  <div class="login">
     
      <div class="login-con">
         <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
         ruleForm: {
          username:"",
          password:""
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ]
        }
      }
    },
     methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
          //  1发送请求传递用户名和密码
           this.$axios.post('login',this.ruleForm)
           .then(res=>{
             console.log(res)
            // 判断 
            if(res.meta.status==200){
              // - token存储到本地
              localStorage.setItem('token',res.data.token)
              // - 跳转到首页 ，提示成功
              this.$router.push('/')
              this.$message(res.meta.msg)
            }else{
              this.$message(res.meta.msg)
            }

           })


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
.login{
  height: 100%;
  background-color: pink;
}
.login-con{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  width: 500px;
  height: 300px;
  background-color: #fff;
}
</style>